Skip to contentMethod: setValue(ELContext, Object, Object, Object)
1: /*
2: * Copyright (c) 2012, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package jakarta.el;
19:
20: import java.beans.FeatureDescriptor;
21: import java.util.Iterator;
22:
23: /**
24: * <p>
25: * An <code>ELResolver</code> for resolving user or container managed beans.
26: * </p>
27: * <p>
28: * A {@link BeanNameResolver} is required for its proper operation. The following example creates an
29: * <code>ELResolver</code> that resolves the name "bean" to an instance of MyBean.
30: *
31: * <pre>
32: * <code>
33: * ELResovler elr = new BeanNameELResolver(new BeanNameResolver {
34: * public boolean isNameResolved(String beanName) {
35: * return "bean".equals(beanName);
36: * }
37: * public Object getBean(String beanName) {
38: * return "bean".equals(beanName)? new MyBean(): null;
39: * }
40: * });
41: * </code>
42: * </pre>
43: *
44: * @since Jakarta Expression Language 3.0
45: */
46: public class BeanNameELResolver extends ELResolver {
47:
48: private BeanNameResolver beanNameResolver;
49:
50: /**
51: * Constructor
52: *
53: * @param beanNameResolver The {@link BeanNameResolver} that resolves a bean name.
54: */
55: public BeanNameELResolver(BeanNameResolver beanNameResolver) {
56: this.beanNameResolver = beanNameResolver;
57: }
58:
59: /**
60: * If the base object is <code>null</code> and the property is a name that is resolvable by the BeanNameResolver,
61: * returns the value resolved by the BeanNameResolver.
62: *
63: * <p>
64: * If name is resolved by the BeanNameResolver, the <code>propertyResolved</code> property of the <code>ELContext</code>
65: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
66: * after this method is called, the caller should ignore the return value.
67: * </p>
68: *
69: * @param context The context of this evaluation.
70: * @param base <code>null</code>
71: * @param property The name of the bean.
72: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
73: * the value of the bean with the given name. Otherwise, undefined.
74: * @throws NullPointerException if context is <code>null</code>.
75: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
76: * exception must be included as the cause property of this exception, if available.
77: */
78: @Override
79: public Object getValue(ELContext context, Object base, Object property) {
80: if (context == null) {
81: throw new NullPointerException();
82: }
83:
84: if (base == null && property instanceof String) {
85: if (beanNameResolver.isNameResolved((String) property)) {
86: context.setPropertyResolved(base, property);
87: return beanNameResolver.getBean((String) property);
88: }
89: }
90:
91: return null;
92: }
93:
94: /**
95: * If the base is null and the property is a name that is resolvable by the BeanNameResolver, the bean in the
96: * BeanNameResolver is set to the given value.
97: *
98: * <p>
99: * If the name is resolvable by the BeanNameResolver, or if the BeanNameResolver allows creating a new bean, the
100: * <code>propertyResolved</code> property of the <code>ELContext</code> object must be set to <code>true</code> by the
101: * resolver, before returning. If this property is not <code>true</code> after this method is called, the caller can
102: * safely assume no value has been set.
103: * </p>
104: *
105: * @param context The context of this evaluation.
106: * @param base <code>null</code>
107: * @param property The name of the bean
108: * @param value The value to set the bean with the given name to.
109: * @throws NullPointerException if context is <code>null</code>
110: * @throws PropertyNotWritableException if the BeanNameResolver does not allow the bean to be modified.
111: * @throws ELException if an exception was thrown while attempting to set the bean with the given name. The thrown
112: * exception must be included as the cause property of this exception, if available.
113: */
114: @Override
115: public void setValue(ELContext context, Object base, Object property, Object value) {
116:• if (context == null) {
117: throw new NullPointerException();
118: }
119:
120:• if (base == null && property instanceof String) {
121: String beanName = (String) property;
122:• if (beanNameResolver.isNameResolved(beanName) || beanNameResolver.canCreateBean(beanName)) {
123: beanNameResolver.setBeanValue(beanName, value);
124: context.setPropertyResolved(base, property);
125: }
126: }
127: }
128:
129: /**
130: * If the base is null and the property is a name resolvable by the BeanNameResolver, return the type of the bean.
131: *
132: * <p>
133: * If the name is resolvable by the BeanNameResolver, the <code>propertyResolved</code> property of the
134: * <code>ELContext</code> object must be set to <code>true</code> by the resolver, before returning. If this property is
135: * not <code>true</code> after this method is called, the caller can safely assume no value has been set.
136: * </p>
137: *
138: * @param context The context of this evaluation.
139: * @param base <code>null</code>
140: * @param property The name of the bean.
141: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code> and
142: * the associated BeanNameResolver is not read-only then the type of the bean with the given name. If the given
143: * bean name was resolved but the associated BeanNameResolver is read-only then {@code null}. Otherwise, undefined.
144: * @throws NullPointerException if context is <code>null</code>.
145: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
146: * exception must be included as the cause property of this exception, if available.
147: */
148: @Override
149: public Class<?> getType(ELContext context, Object base, Object property) {
150: if (context == null) {
151: throw new NullPointerException();
152: }
153:
154: if (base == null && property instanceof String) {
155: if (beanNameResolver.isNameResolved((String) property)) {
156: context.setPropertyResolved(true);
157:
158: /*
159: * No resolver level isReadOnly property for this resolver
160: */
161: if (beanNameResolver.isReadOnly((String) property)) {
162: return null;
163: }
164:
165: return beanNameResolver.getBean((String) property).getClass();
166: }
167: }
168:
169: return null;
170: }
171:
172: /**
173: * If the base is null and the property is a name resolvable by the BeanNameResolver, attempts to determine if the bean
174: * is writable.
175: *
176: * <p>
177: * If the name is resolvable by the BeanNameResolver, the <code>propertyResolved</code> property of the
178: * <code>ELContext</code> object must be set to <code>true</code> by the resolver, before returning. If this property is
179: * not <code>true</code> after this method is called, the caller can safely assume no value has been set.
180: * </p>
181: *
182: * @param context The context of this evaluation.
183: * @param base <code>null</code>
184: * @param property The name of the bean.
185: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
186: * <code>true</code> if the property is read-only or <code>false</code> if not; otherwise undefined.
187: * @throws NullPointerException if context is <code>null</code>.
188: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
189: * exception must be included as the cause property of this exception, if available.
190: */
191: @Override
192: public boolean isReadOnly(ELContext context, Object base, Object property) {
193: if (context == null) {
194: throw new NullPointerException();
195: }
196:
197: if (base == null && property instanceof String) {
198: if (beanNameResolver.isNameResolved((String) property)) {
199: context.setPropertyResolved(true);
200: return beanNameResolver.isReadOnly((String) property);
201: }
202: }
203:
204: return false;
205: }
206:
207: /**
208: * Always returns <code>null</code>, since there is no reason to iterate through a list of one element: bean name.
209: *
210: * @param context The context of this evaluation.
211: * @param base <code>null</code>.
212: * @return <code>null</code>.
213: *
214: * @deprecated This method will be removed without replacement in EL 6.0
215: */
216: @Deprecated(forRemoval = true, since = "5.0")
217: @Override
218: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
219: return null;
220: }
221:
222: /**
223: * Always returns <code>String.class</code>, since a bean name is a String.
224: *
225: * @param context The context of this evaluation.
226: * @param base <code>null</code>.
227: * @return <code>String.class</code>.
228: */
229: @Override
230: public Class<?> getCommonPropertyType(ELContext context, Object base) {
231: return String.class;
232: }
233: }